package cn.hniu.controller;

import cn.hniu.mapper.UsersMapper;
import cn.hniu.pojo.Topics;
import cn.hniu.pojo.Users;
import cn.hniu.pojo.util.PageAll;
import cn.hniu.pojo.util.Result;
import cn.hniu.service.UsersService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/Users")
public class UsersController {

    @Autowired
    private UsersService usersService;
    @Autowired
    private UsersMapper usersMapper;

    //查询用户
    @GetMapping("/findUserInformation/{begin}/{size}")
    @ResponseBody
    public Result findUserInformation(@PathVariable("begin") Integer begin,
                                      @PathVariable("size") Integer size){
        IPage page=usersService.findUserInformation(begin,size);
        if(page.getPages()<begin){
            return new Result(400,"页数超过");
        }else {
            return new Result(200,page);
        }
    }
    //搜索用户
     @GetMapping("findUserByCondition/{data}/{begin}/{size}")
     @ResponseBody
    public Result findUserByCondition(@PathVariable("data") String data,
                                     @PathVariable("begin") Integer begin,
                                     @PathVariable("size") Integer size){

        if(usersService.findUserByCondition(data,begin-1,size).size()==0){
            return new Result(400,"用户没找到");
        }else {
            int list = usersMapper.selectCount(new QueryWrapper<Users>().like("user_name", data));
            PageAll pageAll=new PageAll();
            pageAll.setCurrent(begin);
            pageAll.setPages((int)Math.ceil((double)list/size));
            pageAll.setSize(size);
            pageAll.setTotals(list);
            pageAll.setUsersList(usersService.findUserByCondition(data,begin-1,size));
            return new Result(200,pageAll);
        }
     }
    //添加用户
    @PostMapping("/addUser")
    @ResponseBody
    public Result addUser(@RequestBody Users users){
        List<Users> usersList=usersMapper.selectList(null);
        int num=0;
        for(Users user:usersList){
            if(user.getUser_name().equals(users.getUser_name())){
                num++;
            }
        }
        if(num>0){
            return new Result(400,"标题出现重复");
        }else
        if(usersService.addUser(users)>0){
            return new Result(200,"添加成功");
        }else
        {
            return new Result(400,"添加失败");
        }
    }
    //修改用户
    @PutMapping("/updateUser")
    @ResponseBody
    public Result updateUser(@RequestBody Users users){
         if(usersService.updateUser(users)>0){
             return new Result(200,"修改成功");
         }else {
             return new Result(400,"修改失败");
         }
    }

    //删除用户
    @PutMapping("/deleteUser/{id}")
    @ResponseBody
    public Result deleteUser(@PathVariable Integer id){
            if (usersService.deleteUser(id)>0){
                return new Result(200,"删除成功");
            }else {
                return new Result(400,"删除失败");
            }
    }
}
